text: Make Emoji insertion work properly
authorMatthias Clasen <mclasen@redhat.com>
Wed, 1 May 2019 05:13:52 +0000 (05:13 +0000)
committerMatthias Clasen <mclasen@redhat.com>
Wed, 1 May 2019 05:13:52 +0000 (05:13 +0000)
We are now getting focus-out and focus-in events
when the Emoji chooser is shown and hidden, and
this is causing the text to select-on-entry before
inserting the Emoji, which then deletes the selection.

Avoid this by saving and restoring the selection
when presenting the Emoji chooser.

gtk/gtktext.c

index 75e17e11aaedb247c2131d92c46d05e21a74938e..b5d37bbd7744740a6449b7da82682550025fc70b 100644 (file)
@@ -6596,9 +6596,25 @@ gtk_text_get_tabs (GtkText *self)
   return priv->tabs;
 }
 
+static void
+emoji_picked (GtkEmojiChooser *chooser,
+              const char      *text,
+              GtkText         *self)
+{
+  int current_pos;
+  int selection_bound;
+
+  current_pos = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (chooser), "current-pos"));
+  selection_bound = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (chooser), "selection-bound"));
+
+  gtk_text_set_positions (self, current_pos, selection_bound);
+  gtk_text_enter_text (self, text);
+}
+
 static void
 gtk_text_insert_emoji (GtkText *self)
 {
+  GtkTextPrivate *priv = gtk_text_get_instance_private (self);
   GtkWidget *chooser;
 
   if (gtk_widget_get_ancestor (GTK_WIDGET (self), GTK_TYPE_EMOJI_CHOOSER) != NULL)
@@ -6611,9 +6627,12 @@ gtk_text_insert_emoji (GtkText *self)
       g_object_set_data (G_OBJECT (self), "gtk-emoji-chooser", chooser);
 
       gtk_popover_set_relative_to (GTK_POPOVER (chooser), GTK_WIDGET (self));
-      g_signal_connect_swapped (chooser, "emoji-picked", G_CALLBACK (gtk_text_enter_text), self);
+      g_signal_connect (chooser, "emoji-picked", G_CALLBACK (emoji_picked), self);
     }
 
+  g_object_set_data (G_OBJECT (chooser), "current-pos", GINT_TO_POINTER (priv->current_pos));
+  g_object_set_data (G_OBJECT (chooser), "selection-bound", GINT_TO_POINTER (priv->selection_bound));
+
   gtk_popover_popup (GTK_POPOVER (chooser));
 }